package jym.tan.movielibrary.component.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ScheduledExecutorService;

import jym.tan.movielibrary.configuration.ConfigurationManager;

public class RestartListener implements ActionListener {

	@Override
	public void actionPerformed(ActionEvent e) {
		ScheduledExecutorService schedulerExecutor = Executors.newScheduledThreadPool(2);
		Callable<Process> callable = new Callable<Process>() {

			@Override
			public Process call() throws Exception {
				Process process = null;
				String osName = ConfigurationManager.getInstance().getConfiguration().getRunManager().getOsName().toLowerCase();
				String jarPath = ConfigurationManager.getInstance().getConfiguration().getRunManager().getJarPath();
				if (osName.contains("win")) {
					process = Runtime.getRuntime().exec(new String[] { "cmd", "/c", "start", "/b", "java", "-jar", jarPath });
				} else if (osName.contains("linux")) {
					process = Runtime.getRuntime().exec(new String[] { "java", "-jar", jarPath });
				}

				return process;
			}
		};
		FutureTask<Process> futureTask = new FutureTask<Process>(callable);
		schedulerExecutor.submit(futureTask);

		System.exit(0);
	}
}
